package com.example.prison.controller;

import com.example.prison.config.Response;
import com.example.prison.entity.SystemInfo;
import com.example.prison.protocol.StringUtil;
import com.example.prison.service.ExitInfoService;
import com.example.prison.service.SystemInfoService;
import com.example.prison.service.SystemLogService;
import com.example.prison.util.APIUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

import static com.example.prison.util.APIUtil.combineArrays;
import static com.example.prison.util.APIUtil.getStatusMessage;

@Slf4j
@Api(tags = "网关通信")
@RestController
@RequestMapping("/api")
public class NetworkController {

    @Autowired
    private ExitInfoService exitInfoService;

    @Autowired
    private SystemLogService systemLogService;

    @Autowired
    private SystemInfoService systemInfoService;

    @ApiOperation("获取设备状态log")
    @PostMapping("/getLog")
    public Response<?> getLog(@Parameter(description = "ip")@RequestParam(value = "ip") String ip,
                              @Parameter(description = "post")@RequestParam(value = "post") Integer post) {

        String logstr = APIUtil.getStatusLog(ip,post);
//        log.info("获取设备状态log");
        return Response.success(logstr);
    }

    @SneakyThrows
    @ApiOperation("获取板子类型")
    @GetMapping("/getTypeHexStr")
    public Response<?> getTypeHex(@Parameter(description = "模块地址") @RequestParam(value = "moduleAddress") Integer moduleAddress) {
        SystemInfo info = systemInfoService.one();
        byte[] message = new byte[]{0x03, 0x00, 0x03, 0x00, 0x01};
//        String result = getStatusMessage("192.168.3.140", 8886, moduleAddress, message);
        String result = getStatusMessage(info.getGatewayAddress(), Integer.valueOf(info.getGatewayPort()), moduleAddress, message);
        return Response.success(result);
    }

    @ApiOperation("获取小板电压状态消息")
    @GetMapping("/getSmallStatusHexStr")
    public Response<?> getSmallStatusHex(@Parameter(description = "模块地址") Integer moduleAddress) {
        SystemInfo info = systemInfoService.one();
        byte[] message = new byte[]{0x03, 0x00, 0x01, 0x00, 0x08};
//        String result = getStatusMessage("192.168.3.140", 8886, moduleAddress, message);
        String result = getStatusMessage(info.getGatewayAddress(), Integer.valueOf(info.getGatewayPort()), moduleAddress, message);
        return Response.success(result);
    }

    @ApiOperation("获取大板电压状态消息(本系统无，仅测试)")
    @GetMapping("/getLargeStatusHexStr")
    public Response<?> getLargeStatusHex(@Parameter(description = "模块地址") Integer moduleAddress) {
        SystemInfo info = systemInfoService.one();
        byte[] message = new byte[]{0x03, 0x00, 0x01, 0x00, 0x10};
//        String result = getStatusMessage("192.168.3.140", 8886, moduleAddress, message);
        String result = getStatusMessage(info.getGatewayAddress(), Integer.valueOf(info.getGatewayPort()), moduleAddress, message);
        return Response.success(result);
    }


}
